package com.codejoys.ssm.web;


import com.codejoys.ssm.domain.School;
import com.codejoys.ssm.dto.BaseResult;
import com.codejoys.ssm.dto.PageData;
import com.codejoys.ssm.service.SchoolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
public class SchoolController {
    @Autowired
    SchoolService schoolService;


    @GetMapping("/{id}")
    @ResponseBody
    public School findById(@PathVariable("id") Integer id) {
        School school = schoolService.findById(id);
        return school;
    }

    @PostMapping("/schools")
    @ResponseBody
    public BaseResult insert(School school){
       final Integer id = schoolService.insert(school);
       BaseResult result=new BaseResult(0,"success",id);
        return  result;
    }

    @DeleteMapping("/schools")
    @ResponseBody
    public BaseResult deleteByIds(@RequestParam("ids") Integer[] ids){
        schoolService.deleteById(ids);
        return new BaseResult(0,"success");
    }

    @GetMapping("/schools")
    @ResponseBody
    public BaseResult findPage(@RequestParam("page") Integer page,
                               @RequestParam("limit") Integer limit){
        final PageData<School> pageData = schoolService.findPage(page, limit);

        BaseResult baseResult= BaseResult.success();
        baseResult.setData(pageData);
        return baseResult;
    }

    @GetMapping("/schools/filter")
    @ResponseBody
    public BaseResult findByCondition(School school){
       final  List<School> list = schoolService.findByCondition(school);

       BaseResult baseResult= BaseResult.success();
       baseResult.setData(list);

        return baseResult;
    }



}
